package cfca.seal.dao.main.impl;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.Organization;
import cfca.seal.bean.Seal;
import cfca.seal.bean.SealCert;
import cfca.seal.bean.SealImage;
import cfca.seal.bean.User;
import cfca.seal.common.Page;
import cfca.seal.dao.PageDao;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.IOrganizationDao;
import cfca.seal.exception.CodeException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class OrganizationDao
  implements IOrganizationDao
{
  private static String tableName = "组织机构";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  @Resource
  private PageDao pageDao;

  public boolean insert(Organization organization) throws CodeException { try { String sql = "INSERT INTO ORGANIZATION (ID, CODE, NAME, IDENTIFICATION_TYPE, IDENTIFICATION_NO, PHONE_NO, ADDRESS, WEBSITE, POSTCODE, EMAIL, PARENT_ID, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

      Object[] args = { organization.getId(), organization.getCode(), organization.getName(), organization.getIdentificationType(), organization.getIdentificationNo(), organization.getPhoneNo(), organization.getAddress(), organization.getWebsite(), organization.getPostcode(), organization.getEmail(), organization.getParentId(), new Date() };

      log.debug("insert organization sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1002", tableName + "表插入失败", e);
    } }

  public boolean update(Organization organization) throws CodeException
  {
    try
    {
      String sql = "UPDATE ORGANIZATION set NAME = ?, IDENTIFICATION_TYPE = ?, IDENTIFICATION_NO = ?, PHONE_NO = ?, ADDRESS = ?, WEBSITE = ?, POSTCODE = ?, EMAIL = ?, PARENT_ID = ? WHERE ID = ?";

      Object[] args = { organization.getName(), organization.getIdentificationType(), organization.getIdentificationNo(), organization.getPhoneNo(), organization.getAddress(), organization.getWebsite(), organization.getPostcode(), organization.getEmail(), organization.getParentId(), organization.getId() };

      log.debug("update organization sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1003", tableName + "表更新失败", e);
    }
  }

  public Organization getOrganizationById(String id) throws CodeException
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE ID = ?";
      Object[] args = { id };
      log.debug("select Organization sql by id: " + sql + args);
      return (Organization)this.smartDao.getBean(Organization.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public List<Organization> getAll() throws CodeException
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION";
      log.debug("select all Organization sql: " + sql);
      return this.smartDao.getList(Organization.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public boolean deleteById(String id) throws CodeException
  {
    try {
      String sql = "DELETE FROM ORGANIZATION WHERE ID = ?";
      Object[] args = { id };
      log.debug("delete organization sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }

  public Organization getOrganizationByCode(String code) throws CodeException
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE CODE = ?";
      Object[] args = { code };
      log.debug("select Organization sql by code: " + sql + args);
      return (Organization)this.smartDao.getBean(Organization.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public Page<Organization> getPage(String pageNo) throws CodeException
  {
    try {
      return this.pageDao.dispage(Organization.class, "ORGANIZATION", null, new Object[0], pageNo, 20);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public Organization getOrganizationByInfoForInsert(String code, String name, String identificationType, String identificationNo) throws Exception
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE CODE = ? OR (NAME = ? AND IDENTIFICATION_TYPE = ? AND IDENTIFICATION_NO = ?)";
      Object[] args = { code, name, identificationType, identificationNo };
      log.debug("select Organization sql by code: " + sql + args);
      return (Organization)this.smartDao.getBean(Organization.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public Organization getOrganizationByInfoForUpdate(String code, String name, String identificationType, String identificationNo) throws Exception
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE CODE <> ? AND NAME = ? AND IDENTIFICATION_TYPE = ? AND IDENTIFICATION_NO = ?";
      Object[] args = { code, name, identificationType, identificationNo };
      log.debug("select Organization sql by code: " + sql + args);
      return (Organization)this.smartDao.getBean(Organization.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public int countAdminUserRecord(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM ADMIN_USER WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(AdminUser.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", "管理员表表检索失败", e);
    }
  }

  public int countUsersRecord(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM USERS WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(User.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", "用户表表检索失败", e);
    }
  }

  public int countSealImageRecord(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM SEAL_IMAGE WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(SealImage.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", "印模表表检索失败", e);
    }
  }

  public int countSealRecord(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM SEAL WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(Seal.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", "印章表表检索失败", e);
    }
  }

  public int countSealCertRecord(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM SEAL_CERT WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(SealCert.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", "印章证书表表检索失败", e);
    }
  }

  public List<Organization> getParentOrganizationList() throws Exception
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE PARENT_ID = '' OR PARENT_ID IS NULL";
      log.debug("select ParentOrganizationList sql: " + sql);
      return this.smartDao.getList(Organization.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public int countChildRecordByParenId(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT * FROM ORGANIZATION WHERE PARENT_ID = ?";
      Object[] args = { organizationId };
      List resultList = this.smartDao.getList(Organization.class, sql, args);

      if (resultList == null) {
        return 0;
      }
      return resultList.size();
    }
    catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }
}